<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/11/19 11:36
// +----------------------------------------------------------------------

namespace app\single\command;


use app\single\model\LogLoginModel;
use app\single\model\PayLogModel;
use app\single\model\PlayerOnlineModel;
use think\Command;
use think\Db;

class PlayerOnline extends Command
{
    protected function execute($input, $output)
    {
        $logLoginModel = new LogLoginModel();
        $playerOnlineModel = new PlayerOnlineModel();
        $payLogModel = new PayLogModel();
        $day = date("Ymd",strtotime($this->sDate));
        $eDate = date("Ymd",strtotime($this->eDate));
        while($day <= $eDate){
            //统计登录数据
            $data = $logLoginModel->getLoginInfo($day);
            echo "data=>".json_encode($data,true).__LINE__.PHP_EOL;
            //获取当天充值的玩家
            $payPlayerArr = $payLogModel->where("pay_date", $day)->group("role_id")->column("SUM(rmb) AS rmb", "role_id");

            //获取历史充值玩家
            $hispayPlayerArr = $payLogModel->group("role_id")->column("SUM(rmb) AS rmb", "role_id");

            //前一天
            $beforeDay = date("Ymd", strtotime($day) - 86400);
            //查询昨日玩家的连续登录天数，充值天数
            $beforeData = $playerOnlineModel->where("day", $beforeDay)->column("contin_day,contin_pay", "player_id");
            foreach($data as $vo){
                echo "vo =>".json_encode($vo,true).__LINE__.PHP_EOL;
                $save = [];
                $save["online_time"] = $vo["online_time"]?? 0;
                $save["login_num"] = $vo["login_num"];
                $save["level"] = $vo["level"]??1;
                $save["pay_rmb"] = $vo["rmb"] ?: 0;
                $save["power"] = $vo["fc"]?? 0;
                //历史充值
                if(isset($hispayPlayerArr[$vo["role_id"]])){
                    $save["is_pay"] = 2;
                    $save["contin_pay"] = isset($beforeData[$vo["role_id"]]) ? $beforeData[$vo["role_id"]]["contin_pay"] + 1 : 1;
                }

                //当天充值
                if(isset($payPlayerArr[$vo["role_id"]])){
                    $save["is_pay"] = 1;
                    $save["contin_pay"] = isset($beforeData[$vo["role_id"]]) ? $beforeData[$vo["role_id"]]["contin_pay"] + 1 : 1;
                }
                
                $check = $playerOnlineModel->where(["day" => $day, "player_id" => $vo["role_id"]])->find();
                if($check){
                    $playerOnlineModel->where("id", $check["id"])->update($save);
                }else{
                    $save["day"] = $day;
                    $save["player_id"] = $vo["role_id"];
                    $save["username"] = $vo["accname"];
                    $save["platform"] = $vo["platform"];
                    $save["server_id"] = $vo["server_id"];
                    $save["contin_day"] = isset($beforeData[$vo["role_id"]]) ? $beforeData[$vo["role_id"]]["contin_day"] + 1 : 1;
                    //当天注册
                    if(date("Ymd", $vo["reg_time"]) == $day){
                        $save["is_new"] = 1;
                    }
                    // echo "____dump___.".var_export($data,true).__LINE__.PHP_EOL;
                    $playerOnlineModel->insert($save);
                }
            }
            //日期+1
            $day = date('Ymd',strtotime($day) + 86400);
        }
    }
}